package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class DetailPage extends Utils {
    //注意：这里的url必须是有效的博客ID，若博客ID失效了将会导致当前类下面的所有用例执行失败
    public static String url = DetailUrl;
    public DetailPage() {
        super(url);
    }

    public void checkBlogDetail()
    {
        System.out.println(driver.getCurrentUrl());
        //标题
        String titleText = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
        //时间
        String timeText = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.date")).getText();
        //内容---可能是动态元素！！！
//        String contentText = driver.findElement(By.cssSelector("#h2-u9A6Cu4E0Au5F00u5B66")).getText();
        String contentText = driver.findElement(By.cssSelector("#detail>h2")).getText();
        //编辑
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)"));
        //删除按钮
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)"));

        assert !titleText.isEmpty();
        assert !timeText.isEmpty();
        assert !contentText.isEmpty();

    }

    /**
     * 博客详情页——编辑功能
     */
    public void checkDeailEdit() {
        //通过编辑按钮进入到编辑页面
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).click();

        //检查跳转是否正确url 更新文章
        String updateUrl = driver.getCurrentUrl();
        String updateText = driver.findElement(By.cssSelector("#submit")).getAttribute("value");

        assert updateUrl.contains("update");
        assert updateText.equals("更新文章");

        //检查点击更新按钮继续回到博客详情页
        driver.findElement(By.cssSelector("#submit")).click();

        wait.until(ExpectedConditions.urlContains("blog_list"));
    }

    /**
     * 博客详情页——删除功能
     */
    public void checkDeailDel_dismiss()
    {
        //编辑之后回到的是博客列表页，要手动将页面跳转到博客详情页
        driver.get(url);
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();

        //出现弹窗--等待
        wait.until(ExpectedConditions.alertIsPresent());
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        //取消
        alert.dismiss();

        assert (driver.getCurrentUrl()).equals(url);
    }
    public void checkDeailDel_accept()
    {
        //编辑之后回到的是博客列表页，要手动将页面跳转到博客详情页
        driver.get(url);
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();

        //出现弹窗--等待
        wait.until(ExpectedConditions.alertIsPresent());
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        //确定--跳转到博客列表页
        alert.accept();

        wait.until(ExpectedConditions.urlContains("blog_list"));
    }

}